package com.proxyadvertise.parse;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class ConfigFile {
	
	private static Properties CONFIG_FILE_PROPERTIES = new Properties();
	
	public static void parse() throws Exception{
		
		String configFile = ProxyIp.class.getResource( "/config.properties" ).getPath();
		try {
			configFile = URLDecoder.decode( configFile, "utf-8" );
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		
		Properties p = new Properties();
		
		InputStream is = null;

		try {		
			
			is = new FileInputStream( configFile );
			p.load(is);

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
		ConfigFile.CONFIG_FILE_PROPERTIES = p;
		init();
	}
	
	public static List<String> COORDINATE = new ArrayList<String>();
	public static List<String> VISIT_URLS = new ArrayList<String>();
	
	private static void init() throws Exception{
		//坐标
		String[] coordinates = ConfigFile.getConfigValue( "coordinate" ).split( ";" );
		//网址
		String[] urls = ConfigFile.getConfigValue( "urls" ).split( ";" );
		
		//所有配置的网址
		for( String url : urls ){
			ConfigFile.VISIT_URLS.add( url );
		}
		//所有坐标
		for( String coordinate : coordinates ){
			ConfigFile.COORDINATE.add( coordinate );
		}
		
	}
	
	public static List<String> getAllVisitUrl(){
		return ConfigFile.VISIT_URLS;
	}
	
	public static List<String> getAllCoordinate(){
		return ConfigFile.COORDINATE;
	}
	
	public static String getConfigValue( String key ){
		return ConfigFile.CONFIG_FILE_PROPERTIES.getProperty(key);
	}
	
	public static void main(String[] args) throws Exception {
		ConfigFile.parse();
	}
}
